<HTML>
<HEAD>
<TITLE>JavaScript Source Code 3000:  Games:  Hanoi Solver</TITLE>
<META HTTP-EQUIV="JavaScript Source Code 3000" CONTENT = "no-cache">
<META NAME="date" CONTENT="2000-09-09">
<META NAME="channel" CONTENT="Web Developer">
<META NAME="author" CONTENT="Amin Wong (aminwong@hotmail.com)">
<META NAME="section" CONTENT="Games">
<META NAME="description" CONTENT="Solves the Towers of Hanoi game in the shortest number of moves, demo included.  The delay between moves and the number of rings can be adjusted by the user.  Clever!">

<SCRIPT LANGUAGE="JavaScript">
<!-- Original:  Amin Wong (aminwong@hotmail.com) -->




<!-- Begin
var MAXHEIGHT = 8;
var ndisk;
var timer = null;
var mvfrom = new Array(255);
var mvto = new Array(255);
var mv, imv;
var tower = new Array(3);
var h = new Array(3);
var spc = "        ";
var dname = new Array(
"|",
"111",
"22222",
"3333333",
"444444444",
"55555555555",
"6666666666666",
"777777777777777",
"88888888888888888");

function init() {
if (!timer) {
s = document.formHanoi.disk.options[document.formHanoi.disk.selectedIndex].value;
if (s == "random") {
now = new Date();
ndisk = parseInt(now.getTime() / 1000) % 8 + 1;
delete now;
}
else ndisk = s;
mv = 0;
hanoi(0, 2, 1, ndisk);
for (i = 0; i < 3; i++)
tower[i] = new Array(MAXHEIGHT);
for (i = 0; i < ndisk; i++)
tower[0][i] = ndisk - i;
h[0] = ndisk;
h[1] = h[2] = 0;
imv = 0;
document.formHanoi.display.value = gentower();
timer = window.setTimeout("gennexttower()", document.formHanoi.delay.options[document.formHanoi.delay.selectedIndex].value);
   }
}
function stop() {
if (timer) {
window.clearTimeout(timer);
timer = null;
   }
}
function hanoi(from,to,buf,nmv) {
if (nmv > 1) {
hanoi(from, buf, to, nmv - 1);
mvfrom[mv] = from;
mvto[mv++] = to;
hanoi(buf, to, from, nmv - 1);
}
else {
mvfrom[mv] = from;
mvto[mv++] = to;
   }
}
function gennexttower() {
tower[mvto[imv]][h[mvto[imv]]++] = tower[mvfrom[imv]][--h[mvfrom[imv]]];
document.formHanoi.display.value = gentower();
if (++imv < mv)
timer = window.setTimeout("gennexttower()", document.formHanoi.delay.options[document.formHanoi.delay.selectedIndex].value);
else {
for (i = 0; i < 3; i++)
delete tower[i];
timer = null;
    }
}
function gentower() {
s = " \n";
for (i = MAXHEIGHT - 1; i >= 0; i--) {
for (j = 0; j < tower.length; j++) {
len = i < h[j] ? tower[j][i] : 0;
width = MAXHEIGHT - len;
s += " " + spc.substring(0, width) + dname[len] + spc.substring(0, width);
}
s += "\n";
}
return s+"=======================================================";
}
//  End -->
</script>
</HEAD>

<BODY BGCOLOR=#ffffff vlink=#0000ff>

<BR>
<center>
<table width=600 cellpadding=0 cellspacing=10>
<tr>
<td width=468 align=center>

    
    
</td>
<td width=120 align=center>
    
</td>
</tr>
</table>
<BR>
<BR>
<basefont size=3>
<FONT SIZE="+2" FACE="Helvetica,Arial">
<A HREF="../index.htm" /" TARGET="_top"><FONT COLOR="#0000FF"><b>Home</b></font></A>
<img src="../img/arrow.gif" /img/arrow.gif" height=13 width=7 border=0 alt="}">
<A HREF="index.htm" /games/"><font color="#FF0000"><b>Games</b></font></A>
<img src="../img/arrow.gif" /img/arrow.gif" height=13 width=7 border=0 alt="}">
<FONT COLOR="#006666"><b>Hanoi Solver</b></font></font>
<BR>
<BR>
<table BORDER=0 WIDTH=486 CELLPADDING=3 CELLSPACING=0>
<tr><td><font FACE="helvetica,arial,geneva">
<br>
<br>
<!-- Description --><!--content_start-->
Solves the Towers of Hanoi game in the shortest number of moves, demo included.  The delay between moves and the number of rings can be adjusted by the user.  Clever!
<hr>
</td></tr>
</table>
<!-- Demonstration -->
<center>
<form name=formHanoi>
<textarea name=display rows=10 cols=56></textarea><br>
<table border=0 cellspacing=10 cellpadding=0>
<tr>
<td>Number of disks</td>
<td><select name=disk>
<option selected value=random>Random
<option value=1>1
<option value=2>2
<option value=3>3
<option value=4>4
<option value=5>5
<option value=6>6
<option value=7>7
<option value=8>8
</select></td>
</tr>
<tr>
<td>Movement delay</td>
<td><select name=delay>
<option value=100>100 milliseconds
<option value=200>200 milliseconds
<option value=300>300 milliseconds
<option value=400>400 milliseconds
<option selected value=500>500 milliseconds
<option value=600>600 milliseconds
<option value=700>700 milliseconds
<option value=800>800 milliseconds
<option value=900>900 milliseconds
<option value=1000>1000 milliseconds
</select></td>
</tr>
</table>
<input type=button value="Start" onClick="init();">
<input type=button value="Stop" onClick="stop();">
</form>
</center>
<P>
<P>
<a name="source">
<table BORDER=0 WIDTH=486 CELLPADDING=3 CELLSPACING=0>
<tr><td BGCOLOR=yellow><font FACE="helvetica,arial,geneva"><b>JavaScript Source Code 3000:  Games:  Hanoi Solver</b>
<p>Simply click inside the window below, use your cursor to highlight the script, and copy (type Control-c or Apple-c) the script into a new file in your text editor (such as Note Pad or Simple Text) and save (Control-s or Apple-s).  The script is yours!!!
<br><br></font></td></tr>
<tr><td BGCOLOR=yellow ALIGN=CENTER>
<form NAME="copy">

<DIV align="center">
<input type=button value="Highlight All" onClick="javascript:this.form.txt.focus();this.form.txt.select();">&nbsp;&nbsp;&nbsp;&nbsp;
<INPUT TYPE="text" NAME="total" VALUE="Script Size:  3.51 KB" size=24>
</DIV>

<textarea NAME="txt" ROWS=20 COLS=75 WRAP=VIRTUAL>

&lt;!-- TWO STEPS TO INSTALL HANOI SOLVER:

  1.  Copy the coding into the HEAD of your HTML document
  2.  Add the last code into the BODY of your HTML document  --&gt;

&lt;!-- STEP ONE: Paste this code into the HEAD of your HTML document  --&gt;

&lt;HEAD&gt;

&lt;SCRIPT LANGUAGE="JavaScript"&gt;
&lt;!-- Original:  Amin Wong (aminwong@hotmail.com) --&gt;

&lt;! &gt;
&lt;! &gt;

&lt;!-- Begin
var MAXHEIGHT = 8;
var ndisk;
var timer = null;
var mvfrom = new Array(255);
var mvto = new Array(255);
var mv, imv;
var tower = new Array(3);
var h = new Array(3);
var spc = "        ";
var dname = new Array(
"|",
"111",
"22222",
"3333333",
"444444444",
"55555555555",
"6666666666666",
"777777777777777",
"88888888888888888");

function init() {
if (!timer) {
s = document.formHanoi.disk.options[document.formHanoi.disk.selectedIndex].value;
if (s == "random") {
now = new Date();
ndisk = parseInt(now.getTime() / 1000) % 8 + 1;
delete now;
}
else ndisk = s;
mv = 0;
hanoi(0, 2, 1, ndisk);
for (i = 0; i &lt; 3; i++)
tower[i] = new Array(MAXHEIGHT);
for (i = 0; i &lt; ndisk; i++)
tower[0][i] = ndisk - i;
h[0] = ndisk;
h[1] = h[2] = 0;
imv = 0;
document.formHanoi.display.value = gentower();
timer = window.setTimeout("gennexttower()", document.formHanoi.delay.options[document.formHanoi.delay.selectedIndex].value);
   }
}
function stop() {
if (timer) {
window.clearTimeout(timer);
timer = null;
   }
}
function hanoi(from,to,buf,nmv) {
if (nmv &gt; 1) {
hanoi(from, buf, to, nmv - 1);
mvfrom[mv] = from;
mvto[mv++] = to;
hanoi(buf, to, from, nmv - 1);
}
else {
mvfrom[mv] = from;
mvto[mv++] = to;
   }
}
function gennexttower() {
tower[mvto[imv]][h[mvto[imv]]++] = tower[mvfrom[imv]][--h[mvfrom[imv]]];
document.formHanoi.display.value = gentower();
if (++imv &lt; mv)
timer = window.setTimeout("gennexttower()", document.formHanoi.delay.options[document.formHanoi.delay.selectedIndex].value);
else {
for (i = 0; i &lt; 3; i++)
delete tower[i];
timer = null;
    }
}
function gentower() {
s = " \n";
for (i = MAXHEIGHT - 1; i &gt;= 0; i--) {
for (j = 0; j &lt; tower.length; j++) {
len = i &lt; h[j] ? tower[j][i] : 0;
width = MAXHEIGHT - len;
s += " " + spc.substring(0, width) + dname[len] + spc.substring(0, width);
}
s += "\n";
}
return s+"=======================================================";
}
//  End --&gt;
&lt;/script&gt;
&lt;/HEAD&gt;

&lt;!-- STEP TWO: Copy this code into the BODY of your HTML document  --&gt;

&lt;BODY&gt;

&lt;center&gt;
&lt;form name=formHanoi&gt;
&lt;textarea name=display rows=10 cols=56&gt;&lt;/textarea&gt;&lt;br&gt;
&lt;table border=0 cellspacing=10 cellpadding=0&gt;
&lt;tr&gt;
&lt;td&gt;Number of disks&lt;/td&gt;
&lt;td&gt;&lt;select name=disk&gt;
&lt;option selected value=random&gt;Random
&lt;option value=1&gt;1
&lt;option value=2&gt;2
&lt;option value=3&gt;3
&lt;option value=4&gt;4
&lt;option value=5&gt;5
&lt;option value=6&gt;6
&lt;option value=7&gt;7
&lt;option value=8&gt;8
&lt;/select&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Movement delay&lt;/td&gt;
&lt;td&gt;&lt;select name=delay&gt;
&lt;option value=100&gt;100 milliseconds
&lt;option value=200&gt;200 milliseconds
&lt;option value=300&gt;300 milliseconds
&lt;option value=400&gt;400 milliseconds
&lt;option selected value=500&gt;500 milliseconds
&lt;option value=600&gt;600 milliseconds
&lt;option value=700&gt;700 milliseconds
&lt;option value=800&gt;800 milliseconds
&lt;option value=900&gt;900 milliseconds
&lt;option value=1000&gt;1000 milliseconds
&lt;/select&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;input type=button value="Start" onClick="init();"&gt;
&lt;input type=button value="Stop" onClick="stop();"&gt;
&lt;/form&gt;
&lt;/center&gt;

 

&lt;!-- Script Size:  3.51 KB --&gt;</textarea><br><font FACE="helvetica,arial,geneva"></font></td></tr>

</table>
</form>
</FONT>
</CENTER>


</center>
</body></html>
